/**
	AUTHOR		: S.SUJAN KUMAR
	DATE		: 20 JULY 2005
	DESCRIPTION	: TO SAVE THE SORT LOT BIN INFORMATION INTO INFORMATIVE_BINNING TABLE.
*/
ALTER PROC SP_SAVE_SORT_LOT_BINNING_INFO ( 
	@Lot_ID		BIGINT,
	@Wafer_ID	VARCHAR(50),
	@Test_Time	DATETIME,
	@Good_Dice	INT,
	@Yield		FLOAT,
	@Bin_1		INT,
	@Bin_2		INT,
	@Bin_3		INT,
	@Bin_4		INT,
	@Bin_5		INT,
	@Bin_6		INT,
	@Bin_7		INT,
	@Bin_8		INT,
	@Bin_9		INT,
	@Bin_10		INT,
	@Bin_11		INT,
	@Bin_12		INT,
	@Bin_13		INT,
	@Bin_14		INT,
	@Bin_15		INT,
	@Defect_Density	FLOAT,
	@Bin_Rejected	INT,
	@MRB_LIMIT	INT,
	@RMA_LIMIT	INT
)
AS
BEGIN
	DECLARE @COUNT	INT
	--check whether Lot_ID, Wafer_ID exists or not
	SELECT @COUNT = COUNT(*) FROM INFORMATIVE_BINNING WHERE Lot_ID = @Lot_ID AND Wafer_ID = @Wafer_ID
	
	--wafer id exists for the lot So update it
	IF ( @COUNT > 0 )
	 BEGIN
		--TO CHECK WHETHER THE ASSEMBLY RELEASE IS ISSUED OR NOT FOR THE LOT AND WAFER ID
		SET @COUNT = 0
		SELECT @COUNT = COUNT(*) FROM Assm_Release_Lot_Table ARLT, Assm_Release_Table ART  WHERE Lot_id = @LOT_ID AND Wafer_Id = @WAFER_ID AND ART.Assembly_Release_Id = ARLT.Assembly_Release_Id AND ART.Status <> 'CANCELLED'	
		IF ( @COUNT = 0 ) --ASSEMBLY RELEASE IS NOT ISSUED, CAN UPDATE THIS LOT AND WAFER ID
		 BEGIN
			UPDATE Informative_Binning SET  Test_Time = @Test_Time, Good_Dice = @Good_Dice, Yield = @Yield, Bin_1 = @Bin_1, Bin_2 = @Bin_2, Bin_3 = @Bin_3, Bin_4 = @Bin_4, Bin_5 = @Bin_5, Bin_6 = @Bin_6, Bin_7 = @Bin_7, Bin_8 = @Bin_8, Bin_9 = @Bin_9, Bin_10 = @Bin_10, Bin_11 = @Bin_11, Bin_12 = @Bin_12, Bin_13 = @Bin_13, Bin_14 = @Bin_14, Bin_15 = @Bin_15, Defect_Density = @Defect_Density, Bin_Rejected = @Bin_Rejected, MRB_LIMIT = @MRB_LIMIT, RMA_LIMIT = @RMA_LIMIT WHERE Lot_ID = @Lot_ID AND Wafer_ID = @Wafer_ID
		 END
		/*ELSE -- ASSEMBLY RELEASE IS ISSUED, SO WE R NOT UPDATING THIS LOT AND WAFER ID
		 BEGIN
		 END*/
	 END
	ELSE --wafer id not exists for the lot so insert it
	 BEGIN
		INSERT INTO Informative_Binning(Lot_ID, Wafer_ID, Test_Time, Good_Dice, Yield, Bin_1, Bin_2, Bin_3, Bin_4, Bin_5, Bin_6, Bin_7, Bin_8, Bin_9, Bin_10, Bin_11, Bin_12, Bin_13, Bin_14, Bin_15, Defect_Density, Bin_Rejected, MRB_LIMIT, RMA_LIMIT ) values( @Lot_ID, @Wafer_ID, @Test_Time, @Good_Dice, @Yield, @Bin_1, @Bin_2, @Bin_3, @Bin_4, @Bin_5, @Bin_6, @Bin_7, @Bin_8, @Bin_9, @Bin_10, @Bin_11, @Bin_12, @Bin_13, @Bin_14, @Bin_15, @Defect_Density, @Bin_Rejected, @MRB_LIMIT, @RMA_LIMIT )
	 END
END